package com.shanghaionstar.user.service;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.JoinmeInfo3Service;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.more.response.GetContentDetailResponseType;
import com.onstar.cn.more.response.TermsConditionRes;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 7828wc on 16/7/1.
 */
public class TermsConditionsServices extends BaseService<GetContentDetailResponseType> {
    private Context context;

    public TermsConditionsServices(Context context) {
        this.context = context;
    }

    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public RequestEntity getRequestEntity() {
//        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);;
//        PublicService publicService = new PublicService(Constants.CURRENT_SERVER_URL);
//        requestEntity = publicService.queryHelpList(requestEntity, CategoryType.ONSTAR_SYBZ.value(), new DeviceUtil(context).getAppVersionCode() + "");
//        LinkedHashMap<String, Object> customHeads = new LinkedHashMap<String, Object>();
//        customHeads.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
//        requestEntity.setCustomHeads(customHeads);
////        String method = Thread.currentThread().getStackTrace()[1].getMethodName();
//        requestEntity = HttpInjectUtils.injectHttp(getClass(), "getRequestEntity", requestEntity);
//        return requestEntity;
        return null;
    }

    @Override
    public GetContentDetailResponseType jsonToBean(String json) {
        return null;
    }


    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO) , method = HttpMethod.GET)
    public String getCategoryContent() throws OnStarException {
       JoinmeInfo3Service service = new JoinmeInfo3Service(
                Constants.CURRENT_SERVER_URL);
        RequestEntity entity = NetConnectUtil.getAuthRequestEntity(context);
        entity = service.getLegalDocuments(entity);// categoryType!=null?categoryType.value():""
        entity = HttpInjectUtils.injectHttp(TermsConditionsServices.this.getClass(), "getCategoryContent", entity);
        return BusinessUtil.getResponse(getDatas(entity), context);
    }

    public Observable<List<TermsConditionRes>> getCategoryContentById() {
        return Observable.defer(new Func0<Observable<List<TermsConditionRes>>>() {
            @Override
            public Observable<List<TermsConditionRes>> call() {
                try {
                    return Observable.just((List<TermsConditionRes>) new Gson().fromJson( getCategoryContent(),new TypeToken<ArrayList<TermsConditionRes>>(){}.getType()));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

}
